﻿/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.ui {
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.Page;
	import com.crlclr.common.*;
	import com.crlclr.events.*;
	import com.crlclr.managers.*;
	
	import flash.display.*;
	import flash.events.*;

	public class QControlBar extends Component {

		private var nextBtn:QPButton;
		private var prevBtn:QPButton;
		private var fullBtn:QPButton;
		private var moreBtn:QPButton;
		private var loader:Loader;
		public function QControlBar() {
		}
		protected override function create():void{
			nextBtn = new QPButton();
			prevBtn = new QPButton();
	
			fullBtn = new QPButton("FullscreenSkin");
			
			moreBtn = new QPButton("MoreButttonSkin");
			fullBtn.y=moreBtn.y=25;
			prevBtn.scaleX = -1;
			addChild(nextBtn);
			addChild(prevBtn);
			addChild(moreBtn);
			addChild(fullBtn);
			
			loader = new Loader;
			addChild(loader);
		}
		
		protected override function bind():void {
			nextBtn.addEventListener(MouseEvent.CLICK, clickHander);
			prevBtn.addEventListener(MouseEvent.CLICK, clickHander);
			moreBtn.addEventListener(MouseEvent.CLICK, clickHander);
			fullBtn.addEventListener(MouseEvent.CLICK, clickHander);
			
			var stage:Stage=ResizeableManager.getInstance().stage
			stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHander );
			stage.addEventListener(Event.RESIZE,resizeHander);
			
		}
		protected override function init():void{
			var tooltip1:QToolTip = new QToolTip(moreBtn, "查看相册列表");
			var tooltip2:QToolTip = new QToolTip(nextBtn, "下一页");
			var tooltip3:QToolTip = new QToolTip(prevBtn, "上一页");
			var tooltip4:QToolTip = new QToolTip(fullBtn, "屏幕切换");
		}
			
		private function focusChangeHandler(event:FocusEvent):void{
			trace(event);
		}
		private function activateHandler(event:Event):void{
			trace(event);
		}
		private function keyHander(event:KeyboardEvent):void {
			if (!this.visible) return;
			if (Keys.isDown(37)) {
				dispatchEvent(new BookEvent(BookEvent.PREV));
			}else if(Keys.isDown(39)) {
				dispatchEvent(new BookEvent(BookEvent.NEXT));
			}else if (Keys.isDown(77)) {
				dispatchEvent(new BookEvent(BookEvent.MORE));
			}else if (Keys.isDown(70)) {
				fullBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		private function clickHander(event:MouseEvent):void {
			switch(event.target) {
				case nextBtn :
					dispatchEvent(new BookEvent(BookEvent.NEXT));
					break;
				case prevBtn :
					dispatchEvent(new BookEvent(BookEvent.PREV));
					break;
				case moreBtn :
					dispatchEvent(new BookEvent(BookEvent.MORE));
					break;
				case fullBtn :
					if(stage.displayState!=StageDisplayState.FULL_SCREEN){
						stage.displayState=StageDisplayState.FULL_SCREEN;
					}else {
						stage.displayState=StageDisplayState.NORMAL;
					}
			}
		}
		
		private function resizeHander(event:Event):void {
			if (stage.displayState == StageDisplayState.FULL_SCREEN) {
				fullBtn.Skin.gotoAndStop(2);
			}else {
				fullBtn.Skin.gotoAndStop(1);
			}
		}
		
		public function pageChange(event:BookEvent):void {
			var page:Page = event.page;
			if (page.index == 0) {
				prevBtn.visible = false;
			}else {
				prevBtn.visible = true;
			}
			
			if (page.index == page.book.length-1) {
				nextBtn.visible = false;
			}else {
				nextBtn.visible = true;
			}
			
		}
		
		
		override public function set visible(value:Boolean) : void{
			super.visible=value;
			
			if(value){
				ResizeableManager.getInstance().add(this);
			}else
			{
				ResizeableManager.getInstance().remove(this);
			}
		}
		
		public override function resize(w:int,h:int):void {
			nextBtn.x = w;
			fullBtn.x = w -fullBtn.width;
			nextBtn.y = prevBtn.y = (h - prevBtn.height) / 2;
		}
	}
	
}
